﻿using DefaultScope;
using Fabrikam.Client.DataModel;
using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media.Imaging;

namespace Fabrikam.Client.ViewModels
{
    public class ServiceTicketViewModel : ViewModelBase
    {
      
        private ServiceTickets serviceTicket;

        public ServiceTickets ServiceTicket
        {
            get
            {
                return serviceTicket;
            }
            set
            {
                if (value == serviceTicket)
                    return;

                serviceTicket = value;

                base.RaisePropertyChanged(() => ServiceTicket);
            }
        }

        private Customers customer;

        public Customers Customer
        {
            get
            {
                return customer;
            }
            set
            {
                if (value == customer)
                    return;

                customer = value;

                base.RaisePropertyChanged(() => Customer);
            }
        }
        private BitmapImage image;

        public BitmapImage Image
        {
            get
            {
                return image;
            }
            set
            {
                if (value == image)
                    return;

                image = value;

                base.RaisePropertyChanged(() => Image);
            }
        }
        public ServiceTicketViewModel(ServiceTickets ticket)
        {

            this.ServiceTicket = ticket;

            if (!IsInDesignMode)
                GetCustomerAndImage();


        }

        public async Task GetCustomerAndImage()
        {
            if (this.ServiceTicket == null || !this.ServiceTicket.CustomerID.HasValue)
                return;

            this.Customer = await DataService.Current.GetCustomersById(this.ServiceTicket.CustomerID.Value);

            if (this.Customer == null || !this.Customer.ImageId.HasValue)
                return;

            var custImage = await DataService.Current.GetImagesById(this.Customer.ImageId.Value);

            if (custImage == null || custImage.Image == null)
                return;

            this.Image = await DataService.Current.GetBitmapImageFromImageEntity(custImage);

        }
    }
}
